<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="视频标题
" prop="vtitle">
      <el-input v-model="dataForm.vtitle" placeholder="视频标题
"></el-input>
    </el-form-item>
    <el-form-item label="作者姓名
" prop="author">
      <el-input v-model="dataForm.author" placeholder="作者姓名
"></el-input>
    </el-form-item>
    <el-form-item label="封面图" prop="coverurl">
      <el-input v-model="dataForm.coverurl" placeholder="封面图"></el-input>
    </el-form-item>
    <el-form-item label="作者头像url
" prop="headurl">
      <el-input v-model="dataForm.headurl" placeholder="作者头像url
"></el-input>
    </el-form-item>
    <el-form-item label="用户评论数" prop="commentNum">
      <el-input v-model="dataForm.commentNum" placeholder="用户评论数"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="likeNum">
      <el-input v-model="dataForm.likeNum" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="收藏数" prop="collectNum">
      <el-input v-model="dataForm.collectNum" placeholder="收藏数"></el-input>
    </el-form-item>
    <el-form-item label="视频url" prop="playurl">
      <el-input v-model="dataForm.playurl" placeholder="视频url"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="视频分类ID" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="视频分类ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          vid: 0,
          vtitle: '',
          author: '',
          coverurl: '',
          headurl: '',
          commentNum: '',
          likeNum: '',
          collectNum: '',
          playurl: '',
          createTime: '',
          updateTime: '',
          categoryId: ''
        },
        dataRule: {
          vtitle: [
            { required: true, message: '视频标题不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '作者姓名不能为空', trigger: 'blur' }
          ],
          coverurl: [
            { required: true, message: '封面图不能为空', trigger: 'blur' }
          ],
          headurl: [
            { required: true, message: '作者头像url不能为空', trigger: 'blur' }
          ],
          commentNum: [
            { required: true, message: '用户评论数不能为空', trigger: 'blur' }
          ],
          likeNum: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          collectNum: [
            { required: true, message: '收藏数不能为空', trigger: 'blur' }
          ],
          playurl: [
            { required: true, message: '视频url不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '视频分类ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.vid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.vid) {
            this.$http({
              url: this.$http.adornUrl(`/app/videolist/info/${this.dataForm.vid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.vtitle = data.videoList.vtitle
                this.dataForm.author = data.videoList.author
                this.dataForm.coverurl = data.videoList.coverurl
                this.dataForm.headurl = data.videoList.headurl
                this.dataForm.commentNum = data.videoList.commentNum
                this.dataForm.likeNum = data.videoList.likeNum
                this.dataForm.collectNum = data.videoList.collectNum
                this.dataForm.playurl = data.videoList.playurl
                this.dataForm.createTime = data.videoList.createTime
                this.dataForm.updateTime = data.videoList.updateTime
                this.dataForm.categoryId = data.videoList.categoryId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/videolist/${!this.dataForm.vid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'vid': this.dataForm.vid || undefined,
                'vtitle': this.dataForm.vtitle,
                'author': this.dataForm.author,
                'coverurl': this.dataForm.coverurl,
                'headurl': this.dataForm.headurl,
                'commentNum': this.dataForm.commentNum,
                'likeNum': this.dataForm.likeNum,
                'collectNum': this.dataForm.collectNum,
                'playurl': this.dataForm.playurl,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'categoryId': this.dataForm.categoryId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
